﻿using System.Collections.Generic;

namespace SmartMotion.Core
{
    /// <summary>
    /// IO控制器基类，子类主要是纯IO控制器，比如雷赛的IO卡，IO扩展卡，淘宝串口IO卡，等等
    /// </summary>
    public abstract class IoCtrlBase : BindableBase, IIOCtrl
    {
        protected IoCtrlBase()
        {
            DI = new List<IOIn>();
            DO = new List<IOOut>();
            DIID = new List<string>();
            DOID = new List<string>();
            //CardID = CardID;
            DIInuse = new List<IOIn>();
            DOInuse = new List<IOOut>();
            DIIDInUse = new List<string>();
            DOIDInUse = new List<string>();
        }

        #region IIOCtrl

        public ushort CardID { get; set; }

        public abstract bool InitIoCtrl();

        public abstract bool CloseIoCtrl();

        public List<IOIn> CreateInputIO(int inputCount)
        {
            for (int i = 0; i < inputCount; i++)
            {
                var input = new IOIn()
                {
                    IOIndex = i,
                };
                DI.Add(input);
            }

            return DI;
        }

        public List<IOOut> CreateOutputIO(int outputCount)
        {
            for (int i = 0; i < outputCount; i++)
            {
                var output = new IOOut()
                {
                    IOIndex = i,
                };
                DO.Add(output);
            }

            return DO;
        }

        public abstract int ReadInBit(ushort bitNo);

        public abstract int ReadOutBit(ushort bitNo);

        public abstract int ReadInPort(ushort portNo);

        public abstract int ReadOutPort(ushort portNo);

        public abstract int WriteOutbit(ushort bitNo, ushort onOff);

        public abstract int WriteOutPort(ushort portNo, ushort portValue);

        public List<IOIn> DI { get; }

        public List<IOIn> DIInuse { get; }

        public List<IOOut> DO { get; }

        public List<IOOut> DOInuse { get; }

        public List<string> DIID { get; set; }

        public List<string> DOID { get; set; }

        public List<string> DIIDInUse { get; set; }

        public List<string> DOIDInUse { get; set; }

        #endregion IIOCtrl
    }
}